package com.hok.misc.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPCommand;
import org.apache.commons.net.ftp.FTPFile;

public class FTPUtil {
	
	public static void uploadFile(String host, String userName, String password, String fileName) throws FTPException {
		FTPClient ftpClient = new FTPClient();
		FileInputStream fis = null;
		try {
			ftpClient.connect(host);
			if(!ftpClient.login(userName, password)) {
				throw new FTPException(ErrorCode.user_err.getCode());
			}
			if(fileName == null || "".equals(fileName)) {
				throw new FTPException(ErrorCode.file_not_exist.getCode());
			}
			File srcFile = new File(fileName);
			fis = new FileInputStream(srcFile);
			ftpClient.changeWorkingDirectory("/home/bill/billing/temp");
			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("UTF-8");
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.cwd("temp");
			ftpClient.storeFile(srcFile.getName(), fis);
			System.out.println(srcFile.getName());
		} catch (SocketException e) {
			throw new FTPException(ErrorCode.conn_err.getCode());
		} catch (FileNotFoundException e) {
			throw new FTPException(ErrorCode.file_not_exist.getCode());
		} catch (IOException e) {
			throw new FTPException(ErrorCode.conn_err.getCode());
		} 
		
	}
	
	public static void downloadFile(String host, String userName, String password, String fileName) {
		
	}
	
	public static void deleteFile(String host, String userName, String password, String fileName) {
		
	}
	
	public static FTPFile[] listFile(String host, String userName, String password, String path) throws FTPException {
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.connect(host);
			if(!ftpClient.login(userName, password)) {
				throw new FTPException(ErrorCode.user_err.getCode());
			}
			
			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("UTF-8");
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			
			FTPFile[] files = listFile(ftpClient, path);
			
			return files;
		} catch (SocketException e) {
			throw new FTPException(ErrorCode.conn_err.getCode());
		} catch (FileNotFoundException e) {
			throw new FTPException(ErrorCode.file_not_exist.getCode());
		} catch (IOException e) {
			throw new FTPException(ErrorCode.conn_err.getCode());
		} 
	}
	
	private static FTPFile[] listFile(FTPClient ftpClient, String filePath) throws FTPException {
		try {
			
			FTPFile[] files = null;
			if(filePath == null || "".equals(filePath)) {
				filePath = "";
				files  = ftpClient.listFiles();
			} else {
				ftpClient.cwd(filePath);
				files  = ftpClient.listFiles();
			}
			
			for(FTPFile file : files) {
				if(file.isDirectory()) {
					System.out.println(filePath + "/" +  file.getName());
					listFile(ftpClient, filePath + "/" +  file.getName());
				} else {
					System.out.println(filePath + "/" + file.getName());
				}
				
				
			}
			return files;
		} catch (IOException e) {
			throw new FTPException(ErrorCode.conn_err.getCode());
		}
	}
}
